﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using JWT;
using Zhj.Community.Infrastructure.WebApi;
using Zhj.Community.WebApi.Models;

namespace Zhj.Community.WebApi.Controllers
{

    public class TestApiController : BaseApiController
    {
        public HttpResponseMessage Get()
        {

            //iss：Issuer，发行者
            //sub：Subject，主题
            //aud：Audience，观众
            //exp：Expiration time，过期时间
            //nbf：Not before
            //iat：Issued at，发行时间
            //jti：JWT ID
            var token = new Token()
            {
                Iss = "zhj",
                Userinfo = new Userinfo()
                {
                    Id = 1,
                    Name = "zhj",
                    Age = 18
                }
            };
            

            string str = JsonWebToken.Encode(token, WebSettingsConfig.TokenKey, JwtHashAlgorithm.HS256);
            var resp = new HttpResponseMessage();
            var cookie = new CookieHeaderValue("token", str)
            {
                Expires = DateTimeOffset.Now.AddDays(1),
                Domain = Request.RequestUri.Host,
                Path = "/"
            };
            resp.Headers.AddCookies(new[] { cookie });
            return resp;
        }

    }
}
